package com.jvm;

import java.util.concurrent.CountDownLatch;

/**
 * 项目名称: java_algorithm
 * @ClassName CountDownLatchTest
 * @Description
 * @Author huipanxing huipanxing@chinasofti.com   @Date 2021/10/14 9:27
 */
public class CountDownLatchTest {

    /**
     * CountDownLatch 的构造函数初始化个数量 countDown方法会减一  等到数量为0时  await会被唤醒  否则就阻塞
     */
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch=new CountDownLatch(10);

//        for (int i=1;i<=10;i++){
//            int finalI = i;
//            new Thread(()->{
//                System.out.println("停车开始"+ finalI);
//                try {
//
//                    countDownLatch.await();
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                System.out.println("停车结束"+finalI);
//            }).start();
//        }
//        Thread.sleep(3000);
//        for (int i=1;i<=10;i++){
//            Thread.sleep(2000);
//            int finalI = i;
//            new Thread(()->{
//                System.out.println("取车开始"+ finalI);
//                countDownLatch.countDown();
//                System.out.println("取车结束"+finalI);
//            }).start();
//        }

        for (int i=1;i<=9;i++){
            new Thread(()->{
                countDownLatch.countDown();
                System.out.println("hhh");
            }).start();
        }
        Thread.sleep(2000);
        new Thread(()->{
            countDownLatch.countDown();
            System.out.println("hhh");
        }).start();

        countDownLatch.await();
        System.out.println("完成");
    }
}
